package com.coffee.core.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;


public class DTOUtils {
    private static final Logger logger = LoggerFactory.getLogger(DTOUtils.class);

    private DTOUtils() {
    }

    public static <T, K> List<K> convertList(List<T> origList, Class<K> clazz) {
        List<K> destList = new ArrayList<K>();
        if (origList == null || origList.size() < 1) {
            return destList;
        }
        if (logger.isDebugEnabled()) {
            logger.debug("Converting list in size of " + origList.size());
        }

        try {
            for (T original : origList) {
                K result = clazz.newInstance();
                BeanUtilExt.copyProperties(result, original);
                destList.add(result);
            }
        } catch (ReflectiveOperationException e) {
            e.printStackTrace();
        }
        return destList;
    }

    public static <T> T convertObject(Object orig, Class<T> clazz){
        T result = null;
        if(orig == null){
            return result;
        }
        try {
            result = clazz.newInstance();
            BeanUtilExt.copyProperties(result, orig);
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return result;
    }

}
